import { DbService } from '@app/db';
import { Injectable } from '@nestjs/common';

@Injectable()
export class DictionaryService {
    constructor(public readonly db:DbService) {

    }

    async getItemByCategory(categoryCode:string) {
        const category = await this.db.dictionaryCategory.findOne({code : categoryCode}, ['id'])
        if(!category) return []

        return this.db.dictionaryItem.find({categoryId : category.id})
    }

    async getItemCodeToNameByCategory(categoryCode:string) {
        const map:{[key:string] : string|number} = {}
        const items = await this.getItemByCategory(categoryCode)
        items.forEach(item => {
            map[item.code] = item.name
        })

        return map
    }

    async getItemNameToCodeByCategory(categoryCode:string) {
        const map:{[key:string] : string|number} = {}
        const items = await this.getItemByCategory(categoryCode)
        items.forEach(item => {
            map[item.name] = item.code
        })

        return map
    }
}
